package com.longshine.tools.util;

import java.text.SimpleDateFormat;
import java.util.Date;


public class StringUtil {
	public static String toString(Object o){
		if(o==null) return "";
		return o.toString();
	}
	public static String toJavaString(String str){
		
		String sarr[] = str.split("_");
		String s="";
		String rt="";
		for(int i=0;i<sarr.length;i++){
			if(i==0){
				s = sarr[i].toLowerCase();
				
			}
			else{
				s = toLowerNotFirst(sarr[i]);
			}
			rt+=s;
			sarr[i]=null;
			s=null;
		}
		sarr=null;
		return rt;
	}
	
	
	public static String toLowerString(String s,int start,int len){
		String a = s.substring(start,len).toLowerCase();
		return s.substring(0,start)+a;
	}
	public static String toLowerNotFirst(String s){
		return toLowerString(s,1,s.length());
	}
	
	public static String toDBString(String str){
		byte[] bs = str.getBytes();
		String newString = "";
		int start = 0;
		for(int i=0;i<bs.length;i++){
			if(bs[i]>=65&&bs[i]<65+26){
				newString+=str.substring(start,i).toUpperCase()+"_";
				start = i;
			}
		}
		newString+=str.substring(start,bs.length).toUpperCase();
		return newString;
		
	}
	public static String toUTFString(String str){
		if(str==null) return str;
		if("".equals(str))return str;
		try {
			return str;
		} catch (Exception e) {
			return str;
		}
	}
	public static String dateStringFormat(String date){
		if(date==null) return "";
		date = date.replaceAll("/", "");
		date = date.replaceAll("-", "");
		date = date.replaceAll(":", "");
		date = date.replaceAll(" ", "");
		date = date.replaceAll("T", "");
		return date;
	}
	
	public static Date addYear(Date d,int year){
		try{
			if(d==null)return d;
			d.setYear(d.getYear()+year);
			return d;
		}catch(Exception e){
			return d;
		}
	}
	public static Date addMonth(Date d,int month){
		Date newTime = new Date(d.getYear(),d.getMonth()+month,d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds());
		return newTime;
	}
	public static Date addHour(Date d,int hour){
		Date newTime = new Date(d.getYear(),d.getMonth(),d.getDate(),d.getHours()+hour,d.getMinutes(),d.getSeconds());
		return newTime;
	}
	public static Date addDay(Date d,int day){
		Date newTime = new Date(d.getYear(),d.getMonth(),d.getDate()+day,d.getHours(),d.getMinutes(),d.getSeconds());
		return newTime;
	}
	public static Date addMinute(Date d,int min){
		Date newTime = new Date(d.getYear(),d.getMonth(),d.getDate(),d.getHours(),d.getMinutes()+min,d.getSeconds());
		return newTime;
	}

	
	public static boolean dateDayEqual(String dateStr,Date d){
		String s  = dateStr.substring(0, 8);
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		String dest = sf.format(d);
		return s.equals(dest);
	}
	public static void main(String[] args){
//		System.out.println(addYear(DateUtil.str2DateTime("2010/09-01 01:20:14"),1900));
//		System.out.println(DateUtil.dateToStr(addMonth(DateUtil.str2DateTime("2010/09-01 01:20:14"),10), "yyyyMMddHHmmss"));
		System.out.println(dateDayEqual("20120415",new Date()));
	}
}
